/**
 * Created by Administrator on 2016/11/18.
 */
(function(){
    if($("#login_form").length>0){
    	$("#login_form").on("click", ".captcha",function(){
    		$.post(get_loginUrl()+'Clogin/change_captcha', {}, function(json){
    			$("tr.l_td_w").remove();
        		var cont = '<tr class="l_td_w"> <td><span>验证码</span><input type="text" maxlength="4" name="captcha" placeholder="4位数字"><div class="captcha">'+json['image']+'<a href="javascript:void(0);">换一张</a></div></td></tr>';
        		$(".login-submit").before(cont);
    		}, 'json');
    	});
        $("#login_form").on("click","input[type=submit]",function(e){
            var mobile=$("input[name=mobile]");
            var password=$("input[name=password]");
            if(mobile.val()==""){
                layer.msg('手机号不能为空',{icon:2,time:1000});
                return false;
            }else if(!(/^1\d{10}$/ig.test(mobile.val()))){
                layer.msg('手机号输入有误',{icon:2,time:1000});
                return false;
            }

            if(password.val()==""){
                layer.msg('密码不能为空',{icon:2,time:1000});
                return false;
            }
            
            $.ajax({
		        type:"POST",
		        dataType:"json",
		        async: false,
                url: get_loginUrl()+"Clogin/check_login",
                data: $("form#login_form").serialize(),
                beforeSend: function() {
                    $("#login_form input[type=submit]").val("正在登录...").attr("disabled", true);
                },
		        success: function(json) {
                    if (json.status) {
                    	layer.msg("操作成功,在刷新中", {icon:1,time:1000}, function(){
                    		window.location.href = json.data;
                    	});
                    } else {
                        $("#login_form input[type=submit]").val("立即登录").removeAttr("disabled");
                        layer.msg(json.errmsg, {icon:2,time:1000}, function(){
                        	if(json.data){
                        		if(json.data=='forget'){
                            		if(json.data) {window.location.href = get_loginUrl()+"Cforget/index";}
                            	} else {
                            		$("tr.l_td_w").remove();
                            		var cont = '<tr class="l_td_w"> <td><span>验证码</span><input type="text" maxlength="4" name="captcha" placeholder="4位数字"><div class="captcha">'+json.data['image']+'<a href="javascript:void(0);">换一张</a></div></td></tr>';
                            		$(".login-submit").before(cont);
                            	}
                        	}
                        });
                    }
                }
		    });
			
            e.preventDefault();
        });
    }

})();